﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VSM.Utils
{
    public delegate void TextChangedMethod(object sender, EventArgs e);
    public class GridViewTemplate : ITemplate
    {
        //A variable to hold the type of ListItemType.
        ListItemType _templateType;
        //A variable to hold the column name.
        string _columnName;

        private TextChangedMethod delegateMethod;

        //Constructor where we define the template type and column name.
        public GridViewTemplate(ListItemType type, string colname, TextChangedMethod delegateMethod)
        {
            //Stores the template type.
            _templateType = type;
            //Stores the column name.
            _columnName = colname;

            this.delegateMethod = delegateMethod;
        }

        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            switch (_templateType)
            {
                case ListItemType.Header:
                    //Creates a new label control and add it to the container.
                    Label lbl = new Label();            //Allocates the new label object.
                    lbl.Text = _columnName;             //Assigns the name of the column in the lable.
                    container.Controls.Add(lbl);        //Adds the newly created label control to the container.
                    break;

                case ListItemType.Item:
                    //Creates a new text box control and add it to the container.
                    //TextBox tb1 = new TextBox();                            //Allocates the new text box object.
                    Label lblCol = new Label();
                    lblCol.ID = "lbl" + _columnName;
                    lblCol.Style["text-align"] = "right";
                    lblCol.Attributes.CssStyle.Add("Width", "98%");
                    lblCol.DataBinding += new EventHandler(tb1_DataBinding);   //Attaches the data binding event.
                    container.Controls.Add(lblCol);   
                    //Adds the newly created textbox to the container.
                    break;

                case ListItemType.EditItem:
                    //Creates a new text box control and add it to the container.
                    TextBox txtCol = new TextBox();
                    txtCol.ID = "txt" + _columnName;
                    txtCol.Style["text-align"] = "right";
                    txtCol.Attributes.CssStyle.Add("Width", "95%");
                    //Allocates the new text box object.
                    txtCol.DataBinding += new EventHandler(tb1_DataBinding);   //Attaches the data binding event.
                    txtCol.Columns = 4;                                        //Creates a column with size 4.

                    //Panel pnl = new Panel();
                    //pnl.ID = "pnl" + _columnName;
                    //pnl.Controls.Add(txtCol);
                    //txtCol.AutoPostBack = true;
                    //txtCol.EnableViewState = true;
                    //txtCol.TextChanged += new EventHandler(this.delegateMethod);
                    container.Controls.Add(txtCol);
                    break;

                case ListItemType.Footer:
                    CheckBox chkColumn = new CheckBox();
                    chkColumn.ID = "Chk" + _columnName;
                    container.Controls.Add(chkColumn);
                    break;
            }
        }



        /// <summary>

        /// This is the event, which will be raised when the binding happens.

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        void tb1_DataBinding(object sender, EventArgs e)
        {
            Object dataValue = null;
            GridViewRow container = null;
            if(sender.GetType() == typeof(TextBox))
            {
                TextBox txtdata = (TextBox)sender;
                container = (GridViewRow)txtdata.NamingContainer;
                dataValue = DataBinder.Eval(container.DataItem, _columnName);
                if (dataValue != DBNull.Value)
                {
                    //txtdata.ID = "txt" + _columnName;
                    txtdata.Text = dataValue.ToString();
                }
            }
            else if (sender.GetType() == typeof(Label))
            {
                Label lbldata = (Label)sender;
                container = (GridViewRow)lbldata.NamingContainer;
                dataValue = DataBinder.Eval(container.DataItem, _columnName);
                if (dataValue != DBNull.Value)
                {
                    //lbldata.ID = "lbl" + _columnName;
                    lbldata.Text = dataValue.ToString();
                }
            }
            
            /*
            TextBox txtdata = (TextBox)sender;
            GridViewRow container = (GridViewRow)txtdata.NamingContainer;
            object dataValue = DataBinder.Eval(container.DataItem, _columnName);

            if (dataValue != DBNull.Value)
            {
                txtdata.Text = dataValue.ToString();
            }*/
        }
    }

    /*
    public class GridViewLabelTemplate : ITemplate
    {
       
        private DataControlRowType templateType;
        private string columnName;
        private string dataType;

        public GridViewLabelTemplate(DataControlRowType type, string colname, string DataType)
        {
            templateType = type;
            columnName = colname;
            dataType = DataType;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            DataControlFieldCell hc = null;

            switch (templateType)
            {
                case DataControlRowType.Header:
                    // build the header for this column
                    Literal lc = new Literal();
                    lc.Text = "<b>" + BreakCamelCase(columnName) + "</b>";
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    // build one row in this column
                    Label l = new Label();
                    switch (dataType)
                    {
                        case "DateTime":
                            l.CssClass = "ReportNoWrap";
                            break;
                        case "Double":
                            hc = (DataControlFieldCell)container;
                            hc.CssClass = l.CssClass = "ReportNoWrapRightJustify";
                            break;
                        case "Int16":
                        case "Int32":
                            hc = (DataControlFieldCell)container;
                            hc.CssClass = l.CssClass = "ReportNoWrapRightJustify";
                            break;
                        case "String":
                            l.CssClass = "ReportNoWrap";
                            break;
                    }
                    // register an event handler to perform the data binding
                    l.DataBinding += new EventHandler(this.l_DataBinding);
                    container.Controls.Add(l);
                    break;
                default:
                    break;
            }
        }

        // helper method to convert CamelCaseString to Camel Case String
        // by inserting spaces
        private string BreakCamelCase(string CamelString)
        {
            string output = string.Empty;
            bool SpaceAdded = true;

            for (int i = 0; i < CamelString.Length; i++)
            {
                if (CamelString.Substring(i, 1) ==
                    CamelString.Substring(i, 1).ToLower())
                {
                    output += CamelString.Substring(i, 1);
                    SpaceAdded = false;
                }
                else
                {
                    if (!SpaceAdded)
                    {
                        output += " ";
                        output += CamelString.Substring(i, 1);
                        SpaceAdded = true;
                    }
                    else
                        output += CamelString.Substring(i, 1);
                }
            }

            return output;
        }

        private void l_DataBinding(Object sender, EventArgs e)
        {
            // get the control that raised this event
            Label l = (Label)sender;
            // get the containing row
            GridViewRow row = (GridViewRow)l.NamingContainer;
            // get the raw data value and make it pretty
            string RawValue =
                DataBinder.Eval(row.DataItem, columnName).ToString();
            switch (dataType)
            {
                case "DateTime":
                    l.Text = String.Format("{0:d}", DateTime.Parse(RawValue));
                    break;
                case "Double":
                    l.Text = String.Format("{0:###,###,##0.00}", Double.Parse(RawValue));
                    break;
                case "Int16":
                case "Int32":
                    l.Text = RawValue;
                    break;
                case "String":
                    l.Text = RawValue;
                    break;
            }
        }

    }
    */
}